package com.i7time.qutan.controller;


import com.i7time.entity.FriendInfo;
import com.i7time.entity.Info;
import com.i7time.entity.SceneInfo;
import com.i7time.entity.User;
import com.i7time.qutan.testService.FriendServiceImpl;
import com.i7time.qutan.testService.SceneServiceImpl;
import com.i7time.qutan.utils.ReturnMap;
import com.i7time.qutan.utils.SceneUtil;
import com.i7time.service.FriendService;
import com.i7time.service.InfoService;
import com.i7time.service.SceneService;
import com.i7time.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Hsia on 16/6/2.
 */
@Controller
@RequestMapping("/scene")
public class SceneController {
    private Logger logger=Logger.getLogger(this.getClass());
    private final long limit=500;

    @Autowired
    SceneService sceneService;
//    @Autowired
//    FriendService friendService;
     @Autowired
    InfoService infoService;


//    SceneService sceneService=new SceneServiceImpl();
    FriendService friendService=new FriendServiceImpl();



    /**
     * 签入
     * @return
     */
    @ResponseBody
    @RequestMapping("signIn")
    public Map signIn(@RequestBody Map map){
        try{
        long userid=Long.valueOf((String)map.get("userid"));
        long sceneid=Long.valueOf((String)map.get("sceneid"));
        long signedSceneId=Long.valueOf((String)map.get("signedSceneId"));
        String isSigned=(String)map.get("isSigned");

        double latitude=(double)map.get("latitude");//纬度
        double longtitude=(double)map.get("longtitude");//经度

            HashMap result= ReturnMap.getSuccessMap();
            HashMap signResult=new HashMap<String,String>();

            if(sceneid==signedSceneId && "1".equals(isSigned)){//如果两个场景值相同,且状态为签入直接返回
                signResult.put("isSigned","1");
                signResult.put("signedSceneId",sceneid+"");
                result.put("signResult",signResult);
                return result;


            }

            // 先执行签出逻辑
            if("1".equals(isSigned.trim()) && sceneService.isUserInScene(userid,signedSceneId)){
                sceneService.signOut(userid,signedSceneId);
            }


        long distance=sceneService.getDistance(sceneid,longtitude,latitude);



        if(distance<=limit){

            sceneService.signIn(userid,sceneid);//加到在线人员缓存
            signResult.put("isSigned","1");
            signResult.put("signedSceneId",sceneid+"");

        }else{
            signResult.put("isSigned","0");
            signResult.put("signedSceneId",sceneid+"");

        }
            //Todo:加入聊天室
        result.put("signResult",signResult);


        return result;

        }catch(Exception e){
            logger.error("",e);
            return ReturnMap.exceptionMap;
        }

    }


    /**
     * 签出--用户主动点退出
     * @return
     */
    @ResponseBody
    @RequestMapping("signOut")
    public Map signOut(@RequestBody Map map){
        try{
            long userid=Long.valueOf((String)map.get("userid"));
            long signedSceneId=Long.valueOf((String)map.get("signedSceneId"));
            String isSigned=(String)map.get("isSigned");
            boolean flag=sceneService.isUserInScene(userid,signedSceneId);
            if("1".equals(isSigned) &&sceneService.isUserInScene(userid,signedSceneId)){//签出逻辑
                sceneService.signOut(userid,signedSceneId);
            }


        HashMap result= ReturnMap.getSuccessMap();
        HashMap signResult=new HashMap<String,String>();

            signResult.put("isSigned","0");
            signResult.put("signedSceneId","0");


        result.put("signResult",signResult);

        //Todo:退出聊天室
        return result;

        }catch(Exception e){
            logger.error("",e);
            return ReturnMap.exceptionMap;
        }

    }




    /**
     * 检查签入状态　
     * @return
     */
    @ResponseBody
    @RequestMapping("signCheck")
    public Map signCheck(@RequestBody Map map){
        try{
            long userid=Long.valueOf((String)map.get("userid"));
            long signedSceneId=Long.valueOf((String)map.get("signedSceneId"));
            String isSigned=(String)map.get("isSigned");

            double latitude=(double)map.get("latitude");//纬度
            double longtitude=(double)map.get("longtitude");//经度

            // 如果距离超过限制则执行 签出逻辑
            long distance=sceneService.getDistance(signedSceneId,longtitude,latitude);
            HashMap result= ReturnMap.getSuccessMap();
            HashMap signResult=new HashMap<String,String>();


            if(distance>limit){
                if(sceneService.isUserInScene(userid,signedSceneId)) {
                    sceneService.signOut(userid, signedSceneId);
                }
                signResult.put("isSigned","0");
                signResult.put("signedSceneId",signedSceneId+"");

            }else{
                signResult.put("isSigned","1");
                signResult.put("signedSceneId",signedSceneId+"");

            }
            result.put("signResult",signResult);


            return result;

        }catch(Exception e){
            logger.error("",e);
            return ReturnMap.exceptionMap;
        }

    }






    /**
     * 获取场景列表
     * @return
     */
    @ResponseBody
    @RequestMapping("getSceneList")
    public Map getSceneList(@RequestBody Map map){
        try{
            HashMap result= ReturnMap.getSuccessMap();

            String city=(String)map.get("city");
            Double latitude=(Double)map.get("latitude");
            Double longtitude=(Double)map.get("longtitude");
            int     page=(int)map.get("page");

            List<SceneInfo> sceneList= sceneService.getSceneList(city,latitude,longtitude,page);
            if(null==sceneList){
                sceneList=new ArrayList<SceneInfo>();
            }

            List<Object> returnList=new ArrayList<Object>();

            for (SceneInfo scene : sceneList  ) {
                HashMap<String,String> returnMap=new HashMap<>();
                returnMap.put("sceneid",scene.getId()+"");
                returnMap.put("sceneName",scene.getName());
                returnMap.put("avatar",scene.getAvatar());
                returnMap.put("address",scene.getAddr());
                returnMap.put("liveStatus",sceneService.getLiveStatus(scene.getId())+"");
                returnMap.put("onlinePeoples",sceneService.getOnLinePeoples(scene.getId())+"");
                returnList.add(returnMap);
            }


            result.put("sceneList",returnList);


            return result;

        }catch(Exception e){
            logger.error("",e);
            return ReturnMap.exceptionMap;
        }

    }



    /**
     * 获取场景详情
     * @return
     */
    @ResponseBody
    @RequestMapping("getSceneDetail")
    public Map getSceneDetail(@RequestBody Map map){
        try{
            HashMap result= ReturnMap.getSuccessMap();
            long sceneid=Long.valueOf((String)map.get("sceneid"));


            SceneInfo scene=sceneService.getSceneDetail(sceneid);
            List infoList=infoService.getInfoBySceneId(sceneid);
            String info="暂无信息";
            if(null!=infoList && infoList.size()>0){
                info=((Info)infoList.get(0)).getContent();
            }






            HashMap<String,Object> sceneDetail=new HashMap<>();

            sceneDetail.put("sceneid",scene.getId()+"");
            sceneDetail.put("sceneName",scene.getName());
            sceneDetail.put("avatar",scene.getAvatar());
            sceneDetail.put("address",scene.getAddr());
            sceneDetail.put("liveStatus",sceneService.getLiveStatus(scene.getId())+"");
            sceneDetail.put("chatroomId",scene.getChatroom_id());
            sceneDetail.put("info",info);
            result.put("sceneDetail",sceneDetail);

            return result;

        }catch(Exception e){
            logger.error("",e);
            return ReturnMap.exceptionMap;
        }

    }

    /**
     * 获取在线人员列表
     * @return
     */
    @ResponseBody
    @RequestMapping("getOnlinePeople")
    public Map getOnlinePeople(@RequestBody Map map){
        try{
            HashMap result= ReturnMap.getSuccessMap();
            long sceneid=Long.valueOf((String)map.get("sceneid"));
            long userid=Long.valueOf((String)map.get("userid"));
            List<User> roomMember=sceneService.getRoomMembers(sceneid);
            if(null==roomMember){
                roomMember=new ArrayList<User>();
            }
            List<FriendInfo> friends=friendService.getFriendList(userid);
            if(null==friends){
                friends=new ArrayList<FriendInfo>();
            }

            result.put("peopleList",SceneUtil.memberUsers(roomMember,friends));






            return result;

        }catch(Exception e){
            logger.error("",e);
            return ReturnMap.exceptionMap;
        }

    }



    /**
     * 获取直播人员列表
     * @return
     */
    @ResponseBody
    @RequestMapping("getLivePeople")
    public Map getLivePeople(@RequestBody Map map){
        try{
            HashMap result= ReturnMap.getSuccessMap();
            long sceneid=Long.valueOf((String)map.get("sceneid"));
            long userid=Long.valueOf((String)map.get("userid"));
            List<User> liveMember=sceneService.getRoomLiveMembers(sceneid);
            if(null==liveMember){
                liveMember=new ArrayList<User>();
            }

            List<FriendInfo> friends=friendService.getFriendList(userid);
            if(null==friends){
                friends=new ArrayList<FriendInfo>();
            }



            result.put("peopleLiveList",SceneUtil.memberUsers(liveMember,friends));






            return result;

        }catch(Exception e){
            logger.error("",e);
            return ReturnMap.exceptionMap;
        }

    }







}
